/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package me.panpf.sketch.sample.vt.ui.listcontainer;

import me.panpf.sketch.sample.vt.ResourceTable;
import me.panpf.sketch.sample.vt.utils.PixelUtil;
import me.panpf.sketch.shaper.roundedimageview.ResUtil;

import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

import static ohos.agp.utils.LayoutAlignment.CENTER;

/**
 * LoadingMoreFooter
 *
 * @author hw
 * @since 2021/6/23
 */
public class LoadingMoreFooter extends DirectionalLayout {
    public final static int STATE_LOADING = 0;
    public final static int STATE_COMPLETE = 1;
    public final static int STATE_NOMORE = 2;
    private RoundProgressBar progressView;
    private Text mText;
    private String loadingHint;
    private String noMoreHint;
    private String loadingDoneHint;

    public LoadingMoreFooter(Context context) {
        super(context);
        initView(context, null, null);
    }

    public LoadingMoreFooter(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView(context, attrSet, null);
    }

    public LoadingMoreFooter(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView(context, attrSet, styleName);
    }

    private void initView(Context context, AttrSet attrSet, String styleName) {
        setAlignment(CENTER);
        setOrientation(HORIZONTAL);
        setComponentSize(LayoutConfig.MATCH_PARENT, (int) PixelUtil.vp2px(65));
        progressView = new RoundProgressBar(getContext());
        progressView.setIndeterminate(true);
        progressView.setProgressColor(new Color(getContext().getColor(ResourceTable.Color_colorAccent)));
        progressView.setProgressWidth((int) PixelUtil.vp2px(2));
        LayoutConfig layoutParams = new LayoutConfig((int) PixelUtil.vp2px(20),
                (int) PixelUtil.vp2px(20));
        progressView.setLayoutConfig(layoutParams);
        addComponent(progressView);
        addText();
    }

    /**
     * setProgressStyle
     *
     * @param style style
     */
    public void setProgressStyle(int style) {
    }

    /**
     * getVisibleHeight
     *
     * @return height
     */
    public int getVisibleHeight() {
        LayoutConfig layoutConfig = new LayoutConfig();
        return layoutConfig.height;
    }

    /**
     * setState
     *
     * @param state state
     */
    public void setState(int state) {
        switch (state) {
            case STATE_LOADING:
                progressView.setVisibility(VISIBLE);
                mText.setText(loadingHint);
                this.setVisibility(VISIBLE);
                break;
            case STATE_COMPLETE:
                mText.setText(loadingDoneHint);
                this.setVisibility(HIDE);
                break;
            case STATE_NOMORE:
                mText.setText(noMoreHint);
                progressView.setVisibility(HIDE);
                this.setVisibility(VISIBLE);
                break;
        }
    }

    /**
     * destroy
     */
    public void destroy() {
        if (progressView != null) {
            progressView = null;
        }
    }

    /**
     * setProgressVisibility
     *
     * @param visible visible
     */
    public void setProgressVisibility(int visible) {
        progressView.setVisibility(visible);
    }

    /**
     * setLoadingHint
     *
     * @param hint hint
     */
    public void setLoadingHint(String hint) {
        loadingHint = hint;
    }

    /**
     * setNoMoreHint
     *
     * @param hint hint
     */
    public void setNoMoreHint(String hint) {
        noMoreHint = hint;
    }

    /**
     * setLoadingDoneHint
     *
     * @param hint hint
     */
    public void setLoadingDoneHint(String hint) {
        loadingDoneHint = hint;
    }

    private void addText() {
        mText = new Text(getContext());
        mText.setText(ResUtil.getString(getContext(), ResourceTable.String_xr_listview_loading));
        if (loadingHint == null || loadingHint.equals("")) {
            loadingHint = ResUtil.getString(getContext(), ResourceTable.String_xr_listview_loading);
        }
        if (noMoreHint == null || noMoreHint.equals("")) {
            noMoreHint = ResUtil.getString(getContext(), ResourceTable.String_xr_nomore_loading);
        }
        if (loadingDoneHint == null || loadingDoneHint.equals("")) {
            loadingDoneHint = ResUtil.getString(getContext(), ResourceTable.String_xr_loading_done);
        }
        LayoutConfig layoutParams = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT);
        layoutParams.setMargins((int) PixelUtil.vp2px(5), 0, 0, 0);
        mText.setLayoutConfig(layoutParams);
        mText.setTextAlignment(TextAlignment.CENTER);
        mText.setTextSize((int) PixelUtil.vp2px(14));
        mText.setTextColor(new Color(getContext().getColor(ResourceTable.Color_main_text)));
        addComponent(mText);
    }
}
